package view.macrogui
{
	import events.GuiEvent;
	import events.WorldEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import model.macro.Player;
	import model.macro.Territory;
	
	/**
	 */
	public class SideBar extends Sprite
	{
		static public const WIDTH:Number = 160;
		static public const PADDING:Number = 6;
		static public const BG_COLOR:Number = 0x777777;
		
		private var territory_info:TerritoryInfo;
		private var unit_list:UnitList;
		private var building_list:BuildingList;
		private var _territory:Territory;
		private var _player:Player;
		private var opened:Boolean;
		private var toggle_button:ToggleButton;
		private var tab_selector:TabSelector;
		private var recruitment_pane:RecruitmentPane;
		private var construction_pane:ConstructionPane;
		
		/**
		 *
		 */
		public function SideBar(player:Player = null, territory:Territory = null)
		{
			_player = player;
			_territory = territory;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		/**
		 *
		 */
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			toggle_button = new ToggleButton(12, 64);
			toggle_button.x = -12;
			addChild(toggle_button);
			
			toggle_button.addEventListener(MouseEvent.CLICK, function(e:Event):void
				{
					toggle();
				});
			
			cacheAsBitmap = true;
			
			territory_info = new TerritoryInfo();
			addChild(territory_info);
			
			unit_list = new UnitList();
			addChild(unit_list);
			
			building_list = new BuildingList();
			addChild(building_list);
			
			tab_selector = new TabSelector();
			addChild(tab_selector);
			
			tab_selector.addEventListener(GuiEvent.CHANGE_TAB, function(e:GuiEvent):void
				{
					setTab(e.data as String);
				});
			
			setTab("units");
			
			var recruitment_button:ToggleButton = new ToggleButton(20, 20);
			addChild(recruitment_button);
			recruitment_button.addEventListener(MouseEvent.CLICK, function(e:Event):void
				{
					if (recruitment_pane.opened)
						recruitment_pane.close();
					else
						recruitment_pane.open();
				});
			recruitment_button.x = PADDING;
			recruitment_button.y = 400;
			recruitment_pane = new RecruitmentPane();
			addChild(recruitment_pane);
			construction_pane = new ConstructionPane();
			addChild(construction_pane);
			
			redraw();
			
			y = TopPanel.HEIGHT + PADDING;
			x = stage.stageWidth;
			close();
		}
		
		public function toggle():void
		{
			if (opened)
				close();
			else
				open();
		}
		
		public function open():void
		{
			toggle_button.open();
			opened = true;
			addEventListener(Event.ENTER_FRAME, animate);
		}
		
		public function close():void
		{
			toggle_button.close();
			opened = false;
			addEventListener(Event.ENTER_FRAME, animate);
		}
		
		private function setTab(tab:String):void
		{
			
			unit_list.visible = false;
			building_list.visible = false;
			
			switch (tab.toLowerCase())
			{
				case "units": 
					unit_list.visible = true;
					break;
				case "buildings": 
					building_list.visible = true;
					break;
				default: 
					trace("Unknown tab: " + tab);
			}
		}
		
		private function animate(e:Event):void
		{
			if (opened)
			{
				if (x > stage.stageWidth - WIDTH)
				{
					x -= (x - (stage.stageWidth - WIDTH)) / 8 + 0.5;
				}
				else
				{
					x = stage.stageWidth - WIDTH;
					removeEventListener(Event.ENTER_FRAME, animate);
				}
			}
			else
			{
				if (x < stage.stageWidth)
				{
					x += (x - (stage.stageWidth - WIDTH)) / 10 + 4;
				}
				else
				{
					x = stage.stageWidth;
					removeEventListener(Event.ENTER_FRAME, animate);
				}
			}
		}
		
		/**
		 *
		 * @param	territory
		 */
		public function update(player:Player, territory:Territory):void
		{
			_player = player;
			_territory = territory;
			
			if (territory != null)
			{
				tab_selector.visible = true;
				unit_list.update(territory.units);
				building_list.update(territory.buildings);
			}
			else
			{
				tab_selector.visible = false;
				unit_list.update(null);
				building_list.update(null);
			}
			
			if (stage)
				redraw();
		}
		
		/**
		 * Redraw all the data. Must have a stage.
		 */
		public function redraw():void
		{
			graphics.clear();
			graphics.beginFill(BG_COLOR, 0.8);
			graphics.drawRect(0, 0, WIDTH, stage.stageHeight - y - PADDING);
			graphics.endFill();
			
			territory_info.update(territory);
			unit_list.update((territory == null) ? null : territory.units);
			building_list.update((territory == null) ? null : territory.buildings);
			
			territory_info.x = PADDING;
			unit_list.x = PADDING;
			building_list.x = PADDING;
			tab_selector.x = PADDING;
			
			territory_info.y = PADDING;
			tab_selector.y = territory_info.y + territory_info.getHeight() + PADDING;
			unit_list.y = tab_selector.y + tab_selector.getHeight() + PADDING;
			building_list.y = unit_list.y;
		
			// separators
			//graphics.lineStyle(1, 0xDDDDDD);
			//var separator_width:int = WIDTH - PADDING;
			//var separators:Vector.<int> = new Vector.<int>();
			//if (territory_info.getHeight() > 0)
			//separators.push(territory_info.y + territory_info.getHeight() + PADDING / 2);
			//if (unit_list.getHeight() > 0)
			//separators.push(unit_list.y + unit_list.getHeight() + PADDING / 2);
			//
			//for each (var separator_y:int in separators)
			//{
			//graphics.moveTo(PADDING, separator_y);
			//graphics.lineTo(separator_width, separator_y);
			//}
		}
		
		public function onUnitSelected(e:WorldEvent):void
		{
			unit_list.onUnitSelected(e);
		}
		
		public function onUnitDeselected(e:WorldEvent):void
		{
			unit_list.onUnitDeselected(e);
		}
		
		public function onAllUnitsDeselected(e:WorldEvent):void
		{
			unit_list.onAllUnitsDeselected(e);
		}
		
		public function get player():Player
		{
			return _player;
		}
		
		public function get territory():Territory
		{
			return _territory;
		}
	}

}